<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

/**
 * @brief   消息
 * @param
 * @return
 * @author wanchengniuniu
 * @date 2025/2/27
 *
 */
class Message extends Model
{
    use SoftDelete;

    // 表名
    protected $name = 'user_message';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    const STATUS_NORMAL = 1;//已读
    const STATUS_DEFAULT = 0;//未读

    const TYPE_NONE = 0;//无跳转
    const TYPE_ONE = 1;//用户账单消息
    const TYPE_TWO = 2;//用户合同消息
    const TYPE_THREE = 3;//房东报修消息 工单消息
    const TYPE_FORE = 4;//用户已缴费消息
    const TYPE_FIVE = 5;//公告


    static public function getStatusList($type = 2, $value = 1)
    {
        $data = [
            self::STATUS_NORMAL => '已读',
            self::STATUS_DEFAULT => '未读',
        ];
        if ($type == 1) {
            return $data[$value] ?? '';
        } else {
            return $data;
        }
    }

    /**
     * @brief   添加消息
     * @param
     * @return
     * @author wanchengniuniu
     * @date 2025/2/27
     *
     */
    static public function add($data)
    {
        $info = self::where($data)->find();
        if ($info) {
            return $info['id'];
        }
        $data['status'] = self::STATUS_DEFAULT;
        $data['createtime'] = time();
        $data['updatetime'] = time();
        return (new Message())->insertGetId($data);
    }


}
